package com.example.peibei.ui.fragment;

import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.dingtao.common.core.WDFragment;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.ui.activity.LoginActivity;
import com.example.peibei.ui.activity.MainActivity;
import com.example.peibei.ui.activity.SendDynamicActivity;
import com.example.peibei.ui.activity.SendOrderActivity;
import com.example.peibei.ui.adapter.MyPagerAdapter;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.linxiao.framework.dialog.BottomDialog;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class SquareFragment extends WDFragment {
    @BindView(R.id.slideTablaout)
    SlidingTabLayout slideTablaout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;

    private SPUtils spUtils;
    private String token;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private String[] mTitles = {"推荐", "同城"};

    @Override
    public String getPageName() {
        return "动态Fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_square;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        fragments.add(new DynamicListFragment("1"));
        fragments.add(new DynamicListFragment("2"));
        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager(), fragments, mTitles);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);
        slideTablaout.setViewPager(viewpager);
        viewpager.setCurrentItem(0);
        TextView title = slideTablaout.getTitleView(0);
        title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        TextView title1 = slideTablaout.getTitleView(0);
        TextView title2 = slideTablaout.getTitleView(1);
        title1.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
        title2.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int tabCount = slideTablaout.getTabCount();
                for (int i = 0; i < tabCount; i++) {
                    TextView title = slideTablaout.getTitleView(i);
                    if (i == position) {
                        title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
                    } else {
                        title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick(R.id.iv_send_order)
    public void send() {
        token = spUtils.getString(SpConstant.USER_TOKEN);
        if (TextUtils.isEmpty(token)) {
            intent(LoginActivity.class);
        } else {
//            showDialog();
            intent(SendDynamicActivity.class);
        }
    }
}
